package com.example.asynctackactivitiytest;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private ProgressBar pro1;
    private ProgressBar pro2;
    private ProgressBar pro3;
    private ProgressBar pro4;
    private ProgressBar pro5;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private ProgressBar pro6;
    private ProgressBar pro7;
    private ProgressBar pro8;
    private ProgressBar pro9;
    private ProgressBar pro10;
    private Button btn6;
    private Button btn7;
    private Button btn8;
    private Button btn9;
    private Button btn10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        bindID();
    }

    private void bindID() {
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
        btn6 = (Button) findViewById(R.id.btn6);
        btn7 = (Button) findViewById(R.id.btn7);
        btn8 = (Button) findViewById(R.id.btn8);
        btn9 = (Button) findViewById(R.id.btn9);
        btn10 = (Button) findViewById(R.id.btn10);

        pro1 = (ProgressBar) findViewById(R.id.pro1);
        pro2 = (ProgressBar) findViewById(R.id.pro2);
        pro3 = (ProgressBar) findViewById(R.id.pro3);
        pro4 = (ProgressBar) findViewById(R.id.pro4);
        pro5 = (ProgressBar) findViewById(R.id.pro5);
        pro6 = (ProgressBar) findViewById(R.id.pro6);
        pro7 = (ProgressBar) findViewById(R.id.pro7);
        pro8 = (ProgressBar) findViewById(R.id.pro8);
        pro9 = (ProgressBar) findViewById(R.id.pro9);
        pro10 = (ProgressBar) findViewById(R.id.pro10);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btn10.setOnClickListener(this);
    }

    class DownloadTask extends AsyncTask<Integer, Integer, String>{

        @Override
        protected String doInBackground(Integer... params) {
            System.out.println(params[0]+"......启动");
            String result=params[0]+"";
            int count = 0;
            while(count<10){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count++;
                publishProgress(count,params[0]);
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            System.out.println(result+"......完成");
            int flag = Integer.parseInt(result);
            switch (flag) {
                case 1:
                    btn1.setText("下载完成");
                    btn1.setEnabled(false);
                    break;
                case 2:
                    btn2.setText("下载完成");
                    btn2.setEnabled(false);
                    break;
                case 3:
                    btn3.setText("下载完成");
                    btn3.setEnabled(false);
                    break;
                case 4:
                    btn4.setText("下载完成");
                    btn4.setEnabled(false);
                    break;
                case 5:
                    btn5.setText("下载完成");
                    btn5.setEnabled(false);
                    break;
                case 6:
                    btn6.setText("下载完成");
                    btn6.setEnabled(false);
                    break;
                case 7:
                    btn7.setText("下载完成");
                    btn7.setEnabled(false);
                    break;
                case 8:
                    btn8.setText("下载完成");
                    btn8.setEnabled(false);
                    break;
                case 9:
                    btn9.setText("下载完成");
                    btn9.setEnabled(false);
                    break;
                case 10:
                    btn10.setText("下载完成");
                    btn10.setEnabled(false);
                    break;

                default:
                    break;
            }
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            int flag = values[1];
            switch (flag) {
                case 1:
                    pro1.setProgress(values[0]*10);
                    break;
                case 2:
                    pro2.setProgress(values[0]*10);
                    break;
                case 3:
                    pro3.setProgress(values[0]*10);
                    break;
                case 4:
                    pro4.setProgress(values[0]*10);
                    break;
                case 5:
                    pro5.setProgress(values[0]*10);
                    break;
                case 6:
                    pro6.setProgress(values[0]*10);
                    break;
                case 7:
                    pro7.setProgress(values[0]*10);
                    break;
                case 8:
                    pro8.setProgress(values[0]*10);
                    break;
                case 9:
                    pro9.setProgress(values[0]*10);
                    break;
                case 10:
                    pro10.setProgress(values[0]*10);
                    break;

                default:
                    break;
            }
        }



    }

    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                DownloadTask t1 = new DownloadTask();
                t1.execute(1);
                break;
            case R.id.btn2:
                DownloadTask t2 = new DownloadTask();
                t2.execute(2);
                break;
            case R.id.btn3:
                DownloadTask t3 = new DownloadTask();
                t3.execute(3);
                break;
            case R.id.btn4:
                DownloadTask t4 = new DownloadTask();
                t4.execute(4);
                break;
            case R.id.btn5:
                DownloadTask t5 = new DownloadTask();
                t5.execute(5);
                break;
            case R.id.btn6:
                DownloadTask t6 = new DownloadTask();
                t6.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 6);
                break;
            case R.id.btn7:
                DownloadTask t7 = new DownloadTask();
                t7.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 7);
                break;
            case R.id.btn8:
                DownloadTask t8 = new DownloadTask();
                t8.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 8);
                break;
            case R.id.btn9:
                DownloadTask t9 = new DownloadTask();
                t9.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 9);
                break;
            case R.id.btn10:
                DownloadTask t10 = new DownloadTask();
                t10.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 10);
                break;

            default:
                break;
        }
    }
}
